package com.project.auth.controller.process;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.auth.model.process.ProcessTemplate;
import com.project.auth.service.ProcessTemplateService;
import com.project.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author : XIAOMING
 * @create 2023/5/31 16:07
 */
@Api(value = "审批模板管理", tags = "审批模板管理")
@RestController
@RequestMapping(value = "/admin/process/processTemplate")
public class ProcessTemplateController {

    @Autowired
    private ProcessTemplateService processTemplateService;

    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit) {
        Page<ProcessTemplate> pageParam = new Page<>(page, limit);
        IPage<ProcessTemplate> pageList = processTemplateService.selectPageProcessTemplate(pageParam);
        return Result.ok(pageList);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.ok(processTemplate);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result save(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result updateById(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        processTemplateService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "上传流程定义")
    @PostMapping("uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file) throws IOException {
        String path = new File(ResourceUtils.getURL("classpath").getPath()).getAbsolutePath();
        //上传目录
        File tempFile = new File(path + "/process/");
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        //创建空文件,实现写入
        String originalFilename = file.getOriginalFilename();
        File zipFile = new File(path + "/process/" + originalFilename);
        file.transferTo(zipFile);

        //根据上传地址后续部署流程定义,文件名称为流程定义的默认key
        Map<String, Object> map = new HashMap<>();
        map.put("processDefinitionPath", "process/" + originalFilename);
        map.put("processDefinitionKey", originalFilename.substring(0, originalFilename.lastIndexOf(".")));

        return Result.ok(map);
    }

    @ApiOperation(value = "发布")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id) {
        processTemplateService.publish(id);
        return Result.ok();
    }
}
